(function () {
    var exports = this;

    var $ = null;

    var NUM_TO_ZHONGWEN = {
        1: '一',
        2: '二',
        3: '三',
        4: '四',
        5: '五',
        6: '六',
        7: '七',
        8: '八',
        9: '九',
        10: '十',
        11: '十一',
        12: '十二',
        13: '十三',
        14: '十四',
        15: '十五',
        16: '十六',
        17: '十七',
        18: '十八',
        19: '十九',
        20: '二十'
    };

    // var players_4_conf = [14, 38, 62, 85];

    var ZongJiesuanLayer = cc.Layer.extend({
        onEnter: function () {
            cc.Layer.prototype.onEnter.call(this);
        },
        ctor: function (data) {
            this._super();

            var that = this;

            var size = cc.winSize;

            var scene = loadNodeCCS(res.ZongJiesuanLayer_json, that);
            // this.addChild(scene.node);
            $ = create$(this.getChildByName("Scene"));

            var players = data.players;
            this._data = data;

            $('root.panel.lb_ts').setString(timestamp2time(data['ts']));
            $('root.panel.lb_wanfa').setString(mapId2Name(gameData.mapId));
            $('root.panel.lb_roomid').setString('房号: ' + gameData.roomId + ' 局数: ' + data['cur_round'] + '/' + data['total_round']);

            var maxScore = 0;
            for(var k = 0; k < players.length; k++){
                if(maxScore < players[k].score)
                    maxScore = players[k].score;
            }
            var myTotalScore = 0;
            for (var i = 0; i < players.length; i++) {
                var player = players[i];
                var uid = player['uid'];
                var zimo = player['zimo'];
                var jiepao = player['jiepao'];
                var dianpao = player['dianpao'];
                var angang = player['angang'];
                var minggang = player['minggang'];
                var score = player['score'];
                var score_list = player['scores'] || [1,12,13,114,115,6,7,8];

                var playerInfo = gameData.getPlayerInfoByUid(uid);
                if (!playerInfo)
                    continue;

                var head = $('root.panel.row' + i + '.info.head');
                var lbNickname = $('root.panel.row' + i + '.info.lb_nickname');
                var lbUid = $('root.panel.row' + i + '.info.lb_uid');
                var lbScore = $('root.panel.row' + i + '.lb_score');

                loadImageToSprite(playerInfo.headimgurl, head);
                // loadImage(playerInfo.headimgurl, head, false);
                // console.log("headimgurl====:" + player.headimgurl);
                // console.log("head ======:" + head);
                lbNickname.setString(ellipsisStr(playerInfo.nickname,7));
                lbUid.setString('ID: ' + playerInfo.uid);

                // if (player.uid == gameData.uid) {
                //     lbNickname.setColor(cc.color(247,226,131));
                // }

                if (window.inReview)
                    lbUid.setVisible(false);

                var sv = $('root.panel.row' + i + '.sv');
                var row0 = $('row0', sv);
                row0.setVisible(false);
                var totalheight = row0.getContentSize().height * score_list.length;
                if (totalheight < sv.getContentSize().height)
                    totalheight = sv.getContentSize().height;
                sv.setInnerContainerSize(cc.size(sv.getContentSize().width, totalheight));
                for (var j = 0; j < score_list.length; j++) {
                    var row = $('row' + j, sv);
                    if (!row) {
                        row = duplicateNode(row0,true);
                        row.setName('row' + j);
                        sv.addChild(row);
                    }
                    row.setVisible(true);
                    row.setPosition(0, totalheight - j * row0.getContentSize().height);
                    $('txt_jushu', row).setString('第' + NUM_TO_ZHONGWEN[j + 1] + '局：');
                    $('txt_score', row).setString(score_list[j]);
                    // $('separate', row).setVisible(j > 0);
                }


                // if (score < 0) {
                //     $('root.panel.row' + i + '.score_bg_lose').setVisible(true);
                //     $('root.panel.row' + i + '.score_bg_win').setVisible(false);
                // } else {
                //     $('root.panel.row' + i + '.score_bg_lose').setVisible(false);
                //     $('root.panel.row' + i + '.score_bg_win').setVisible(true);
                // }
                
                //todo 房主
                if(i==0 && !data.is_daikai){
                    $('root.panel.row' + i + '.fz').setVisible(true);
                }
                $('root.panel.row' + i + '.dyj').setVisible(maxScore <= score && score > 0);

                if (player.uid == gameData.uid) {
                    myTotalScore = player.score;
                }
                if(score>0){
                    score='+'+score;
                }
                lbScore.setString(''+score);
                lbScore.setFntFile(score>=0?majiang_yz.res.yingFnt:majiang_yz.res.shuFnt);
            }

            for (; i < 4; i++) {
                $('root.panel.row' + i).setVisible(false);
            }
            var count= 1280;
            var interval = count / players.length;
            if (players.length == 2) {
                var w =$('root.panel.row1').getPositionX() - $('root.panel.row0').getPositionX();
                interval=(count-2*w)/3+w;
            }
            var originPosX =640 - (players.length-1)/2 * interval;
            for (var s = 0; s < players.length; s++) {
                $('root.panel.row' + s).setPositionX(originPosX + interval*s);
            }

            playEffect(myTotalScore >= 0 ? 'vWin' : 'vLose');

            var btnShare = $('root.panel.btn_share');

            if (gameData.loginType == 'yk') {
                btnShare.setVisible(false);
            }

            TouchUtil.setOnclickListener(btnShare, function () {
                ShareUtil.SharePic(that);
            });

            TouchUtil.setOnclickListener($('root.panel.btn_back'), function () {
                showBoard('HallBoard');
            });

            TouchUtil.setOnclickListener($('root.panel.btn_mingpian'), function () {
                that._clickSharelianjie();
            });

            // if((data.map_id == MAP_ID_SHANXI_DADIANZI || data.map_id == MAP_ID_SHANXI_HUASHUI) && players.length == 4){
            //     activitySxqyqhd(gameData.roomId);
            // }

            return true;
        },

        /**
         *分享
         * @private
         */
        _clickSharelianjie: function () {
            var self = this;
            var players = deepCopy(self._data.players);
            players = players.sort(function (a, b) {
                return a.score < b.score;
            });
            var fenshu = "";
            for (var i = 0; i < players.length; i++) {
                var player = players[i];
                var uid = player['uid'];
                var score = player['score'];
                var playerInfo = gameData.getPlayerInfoByUid(uid);
                fenshu = fenshu + playerInfo.nickname + ":【" + score + "】,";
            }
            fenshu = fenshu.substring(0, fenshu.length - 1);

            var players = self._data.players;
            ShareUtil.ShareZhanJi(gameData.roomId, players[0]['uid'], players[1]['uid'], this, fenshu);

        },
    });

    exports.ZongJiesuanLayer = ZongJiesuanLayer;
})(window);
